const mysql = require("mysql")
const config = require("../config")

class DB {
    connect = null
    constructor() {
        this.connect = mysql.createConnection(config.db)
    }

    link () {
        return new Promise((resolve, reject) => {
            this.connect.connect(err => {
                if (err) {
                    reject(err)
                } else {
                    resolve()
                }
            })
        })
    }

    query (sql) {
        return new Promise((resolve, reject) => {
            this.connect.query(sql, (err, result) => {
                if (err) {
                    reject(err)
                } else {
                    resolve(result)
                }
            })
        })
    }

    end () {
        this.connect.end()
    }
}

const db = new DB()
db.link()
module.exports = db